define(function () {
    var self,app = win.app;
    var orderItem,userInfo,vue;

    app.newModule({
        init:function () {
            self = this;
            self.mData = {
                troubleItem: [],
                shengList: [],
                shiList: [],
                quList: [],
                MMRD001:'2'
            };
            self.imgArr = [];
            orderItem = app.pageParameters['fwyy-info'];
            if (orderItem == undefined){
                orderItem = app.pageParameters['azyy-info'];
                self.mData.MMRD001 = '1';
            }

            userInfo = app.userInfos.sysUser;

            vue = {
                tags: [],
                ind: '',
                order: orderItem,//设备信息
                form: {
                    MMRD003: orderItem.MMRA003,//设备序号
                    MMRD002: orderItem.MMRA002,//设备编号
                    MMRD001: self.mData.MMRD001,//任务类型
                    MMRD013: '',//故障代码 vue.tags.mmrll001
                    MMRD013List: [],
                    MMRD014: '',//故障描述
                    MMRD015: '',//故障照片
                    MMRD010: orderItem.GWAL021,//联系人
                    MMRD011: orderItem.GWAL020,//联系人电话
                    MMRD006: '',//省
                    MMRD007: '',//市
                    MMRD008: '',//区
                    MMRD012: orderItem.GWAL018, //详细服务地址
                    MMRD004: '',//服务日期
                    MMRD005: '全天',//服务时间
                    MMRD019: '2',//紧急程度
                    MMRD018: userInfo.MOBILE, //把电话号码记录进来
                    GWAKL003:orderItem.GWAKL003,
                    GWAM007:orderItem.GWAM007
                },
                imgs: self.imgArr
            },
            self._super({
                _tag: 'fwyy-info',
                _readyBefore:function () {

                }
            }, win);
        },
        _ready:function () {

            new Vue({
                el: app.getByid(doc, "content"),
                data: vue,
                created : function(){
                    app.ajax({
                        xurl: "api/wx/fwyy/getTags",
                        xdata: {
                            MMRA002:vue.form.GWAM007
                        },
                        cb: function (result) {
                            if (result.ok) {
                                vue.tags = result.data.data;
                            }
                        }
                    });
                },
                watch: {
                    //监听省
                    'form.MMRD006': function () {
                        app.ajax({
                            xurl: '',
                            msg: '',
                            xdata: {
                                MMRD006: vue.form.MMRD006
                            },
                            cb: function (result) {
                                vue.form.MMRD007 = result.data;

                            }
                        })
                    },
                    //监听市
                    'form.MMRD007': function () {
                        if (form.MMRD007 == '') {
                            self.mData.quList.splice(0, self.mData.quList.length);
                        } else {
                            app.ajax({
                                xurl: '',
                                msg: '',
                                xdata: {
                                    MMRD007: vue.form.MMRD007
                                },
                                cb: function (result) {
                                    self.mData.quList.splice(0, self.mData.quList.length);
                                    self.mData.quList = result.data;

                                }
                            })
                        }

                    }
                },
                methods: {
                    btn_MMRD002 : function(){
                        app.scanQRCode(function(result){
                            vue.form.MMRD002 = result;
                        });
                    },
                    btn_MMRD003 : function(){
                        app.scanQRCode(function(result){
                            vue.form.MMRD003 = result;
                        });
                    },
                    status: function(index){
                        var has = false;
                        for(var i=0;i<vue.form.MMRD013List.length;i++){
                            if(vue.tags[index].MMRL001 == vue.form.MMRD013List[i]){
                                has = true;
                            }
                        }
                        return has == true ? 'active':'';
                    },
                    checkeds: function (index) {
                        var has = false;
                        for(var i=0;i<vue.form.MMRD013List.length;i++){
                            if(vue.tags[index].MMRL001 == vue.form.MMRD013List[i]){
                                vue.form.MMRD013List.splice(i,1);
                                has = true;
                                break;
                            }
                        }
                        if(has == false){
                            vue.form.MMRD013List.push(vue.tags[index].MMRL001);
                        }
                        vue.form.MMRD013 = vue.form.MMRD013List.join(',');
                    },
                    showMap: function () {
                        app.openPage({
                            src: "../../html/wx/map.html",
                            argsKey: 'map_page', // 设置页面请求参数key值
                            args: {
                                cb: function (result) {
                                    // 更新数据
                                    vue.form.MMRD012 = result.address;
                                }
                            }
                        });
                    },
                    zhibao: function (value) {
                        if (value == 1) {
                            return '是';
                        } else if (value == 0) {
                            return '否';
                        }else if (value == 2) {
                            return '延保';
                        }
                    },
                    delimg: function (i) {
                        self.imgArr.removei(i);
                    },
                    addImgs: function () {
                        app.chooseImageNew(function (imgs) {
                            //self.imgArr.pushArray(imgs);
                            //app.extend(self.imgArr, imgs);
                            for (var i = 0; i < imgs.length; i++) {
                                self.imgArr.push({
                                    img: imgs[i].img,
                                    id: imgs[i].localId
                                })
                            }
                        });
                    }
                }
            });



            app.taps.call(win, [
                {
                    o: 'submitDiv',
                    cb: function () {
                        if (vue.form.MMRD002 == '') {
                            app.toast('设备编号不能为空');
                            return;
                        }
                        if (vue.form.MMRD013List.length == 0) {
                            app.toast('故障类型没有选择');
                            return;
                        }
                        if (vue.form.MMRD010 == '') {
                            app.toast('联系人不能为空');
                            return;
                        }
                        if (vue.form.MMRD011 == '') {
                            app.toast('联系人电话不能为空');
                            return;
                        }
                        if (vue.form.MMRD004 == '') {
                            app.toast('预约服务日期不能为空');
                            return;
                        }

                        app.showLoading();
                        app.uploadLocalIds({
                            ids:self.imgArr,
                            cb:function (imgs) {
                                var imgurl = "";
                                for (var i = 0; i < imgs.length; i++) {
                                    imgurl += (imgs[i].name + ",");
                                }
                                vue.form.MMRD015=imgurl.substring(0,imgurl.length-1);
                                //vue.form.MMRD015 = imgs.join(',');
                                app.ajax({
                                    xurl: "api/wx/fwyy/saveFwyy",
                                    loading: true,
                                    xdata: vue.form,
                                    cb: function (result) {
                                        app.hideLoading();
                                        if (result.ok) {
                                            app.toast('服务预约成功！');
                                            setTimeout(function () {
                                                mui.back();
                                            }, 600);
                                        } else {
                                            app.toast(result.data.message);
                                        }
                                    }
                                });
                            }
                        });

                    }
                },
            ]);
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
            });
        },
        _lazy:function () {
            this._super();
        }
    })
})
